//
//  CircleManager.m
//  Project5
//
//  Created by Nguyen Tan Loc on 5/18/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "CircleManager.h"

@implementation CircleManager

-(id) init
{
    if (self = [super init])
    {
//        CircleIndicator *ci = [[CircleIndicator alloc] initWithSegment:50 radius:100];
//        CircleIndicator *ci1 = [[CircleIndicator alloc] initWithSegment:50 radius:200];
//        CircleIndicator *ci2 = [[CircleIndicator alloc] initWithSegment:50 radius:300];
//
//        [self addChild:ci];
//        [self addChild:ci1];
//        [self addChild:ci2];
        
        m_ArrCircle = [[NSMutableArray alloc] init];
        m_ArrCircleIndicator = [[NSMutableArray alloc] init];
        
        m_CILayer = [CCNode node];
        [self addChild:m_CILayer];
        
        m_BatchNode = [CCSpriteBatchNode batchNodeWithFile:@"circle.png"];
        [self addChild:m_BatchNode];

        [self scheduleUpdate];
    }

    return self;
}

-(void) addSpriteToBatch
{
    for (int i = 0; i < m_ArrCircle.count; i++)
    {
        Circle *c = [m_ArrCircle objectAtIndex:i];
        [c addSpriteToBatch:m_BatchNode];
    }
}

-(void) update:(ccTime)delta
{
    for (int i = 0; i < m_ArrCircle.count; i++)
    {
        Circle *c = [m_ArrCircle objectAtIndex:i];
        if (c._isDestroy) continue;
        
        [c update:delta];
    }
}

-(void) loadLevel:(NSString *)path
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:path ofType:@"xml"];
    NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath];
    NSError *error;
    
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    if (doc == nil)
        CCLOG(@"doc = nil");

    NSArray *arrCI = [doc.rootElement elementsForName:@"circleindicator"];
    NSArray *arrCircle = [doc.rootElement elementsForName:@"circle"];
    NSArray *arrConfig = [doc.rootElement elementsForName:@"config"];
    
    [self loadCircleIndicator:arrCI];
    [self loadCircle:arrCircle];
    [self loadConfig:arrConfig];
    
    [self addSpriteToBatch];
}

-(void) loadConfig:(NSArray *)arrConfig
{
    for (GDataXMLElement *cf in arrConfig)
    {
        [LevelConfig sharedLevelConfig]._gMode = [[[cf attributeForName:@"mode"] stringValue] intValue];
        [LevelConfig sharedLevelConfig]._gLevelName = [[cf attributeForName:@"name"] stringValue];
        [LevelConfig sharedLevelConfig]._gLimitType = [[[cf attributeForName:@"limittype"] stringValue] intValue];
        [LevelConfig sharedLevelConfig]._gLimitNum = [[[cf attributeForName:@"limitnum"] stringValue] intValue];
        
        for (int i = 0; i < 5; i++)
        {
            float ratebullet = [[[cf attributeForName:[NSString stringWithFormat:@"ratebullet%d", i]] stringValue] floatValue];
            [[LevelConfig sharedLevelConfig]._arrRateBullet addObject:[NSNumber numberWithFloat:ratebullet]];
        }
        
        for (int i = 0; i < 4; i++)
        {
            int score = [[[cf attributeForName:[NSString stringWithFormat:@"score%d", i]] stringValue] intValue];
            [[LevelConfig sharedLevelConfig]._arrScore addObject:[NSNumber numberWithFloat:score]];
        }
        
        for (int i = 0; i < 3; i++)
        {
            int combotype = [[[cf attributeForName:[NSString stringWithFormat:@"combotype%d", i]] stringValue] intValue];
            int comboreq = [[[cf attributeForName:[NSString stringWithFormat:@"comboreq%d", i]] stringValue] intValue];
            
            [[LevelConfig sharedLevelConfig]._arrCombo addObject:[NSNumber numberWithFloat:combotype]];
            [[LevelConfig sharedLevelConfig]._arrCombo addObject:[NSNumber numberWithFloat:comboreq]];
        }
        
        for (int i = 0; i < 3; i++)
        {
            int ingretype = [[[cf attributeForName:[NSString stringWithFormat:@"ingretype%d", i]] stringValue] intValue];
            int ingrereq = [[[cf attributeForName:[NSString stringWithFormat:@"ingrereq%d", i]] stringValue] intValue];
            
            [[LevelConfig sharedLevelConfig]._arrIngre addObject:[NSNumber numberWithFloat:ingretype]];
            [[LevelConfig sharedLevelConfig]._arrIngre addObject:[NSNumber numberWithFloat:ingrereq]];
        }
    }
}

-(void) loadCircleIndicator:(NSArray *)arrCI
{
    for (GDataXMLElement *cie in arrCI)
    {
        int numCI = [[[cie attributeForName:@"numci"] stringValue] floatValue]; CCLOG(@"numCI = %d", numCI);
        int numCIOS = [[[cie attributeForName:@"numcios"] stringValue] floatValue]; CCLOG(@"numCI = %d", numCI);
        
        for (int i = 0; i < numCI; i++)
        {
            float cios = [[[cie attributeForName:[NSString stringWithFormat:@"ci%d", i]] stringValue] floatValue];
            
            CircleIndicator *ci = [[CircleIndicator alloc] initWithSegment:50 radius:cios];
            [ci setID:i];
            [ci setVisible:(i >= numCIOS ? NO : YES)];
            [m_CILayer addChild:ci];
            [m_ArrCircleIndicator addObject:ci];
            [ci release];
        }
    }
}

-(void) loadCircle:(NSArray *)arrCircle
{
    for (GDataXMLElement *ec in arrCircle)
    {
        int ciid = [[[ec attributeForName:@"ciid"] stringValue] intValue];
        float ca = [[[ec attributeForName:@"angle"] stringValue] floatValue];
        float crs = [[[ec attributeForName:@"rotspeed"] stringValue] floatValue];
        CCLOG(@"ciid = %d", ciid);
        CCLOG(@"radius = %f", [self getRadiusWithCIID:ciid]);
        Circle *c = [[Circle alloc] initWithRadius:[self getRadiusWithCIID:ciid] angle:ca];
        [c setCIID:ciid];
        [c setRotationSpeed:crs];
        [m_ArrCircle addObject:c];
        
        NSArray *arrRound = [ec elementsForName:@"round"];
        for (GDataXMLElement *ed in arrRound)
        {
            int st = [[[ed attributeForName:@"type"] stringValue] intValue];
            float rr = [[[ed attributeForName:@"radius"] stringValue] intValue];
            float rrs = [[[ed attributeForName:@"rotspeed"] stringValue] floatValue];
            
            NSMutableArray *listSegment = [NSMutableArray array];
            [listSegment addObject:[NSNumber numberWithInt:st]];
            
            NSArray *arrSegment = [ed elementsForName:@"segment"];
            for (GDataXMLElement *es in arrSegment)
            {
                int sc = [[[es attributeForName:@"color"] stringValue] intValue];
                [listSegment addObject:[NSNumber numberWithInt:sc]];
            }
            
            [c addRoundWithRadius:rr rotationSpeed:rrs listSegment:listSegment];
        }
//        [c appear];
        [c release];
    }
}

-(void) checkCollideWithBullet:(NSArray *)arrBullet
{
    for (int i = 0; i < arrBullet.count; i++)
    {
        Bullet *b = [arrBullet objectAtIndex:i];
        for (int j = 0; j < m_ArrCircle.count; j++)
        {
            Circle *c = [m_ArrCircle objectAtIndex:j];
            if (c._isDestroy) continue;
            if ([c checkCollideWithBullet:b] == YES)
            {
                if ([self checkAllCircleDestroyWithID:c._CIID] == YES)
                {
                    CircleIndicator *ci;
                    ci = [m_ArrCircleIndicator objectAtIndex:[self getIndexWithCIID:c._CIID]];
                    [ci destroy];
                    
                }
            }
        }
    }
}

-(BOOL) checkAllCircleDestroyWithID:(int)ciid
{
    for (int i = 0; i < m_ArrCircle.count; i++)
    {
        Circle *c = [m_ArrCircle objectAtIndex:i];
        if (c._CIID == ciid && c._isDestroy ==  NO)
            return NO;
    }
    return YES;
}

-(int) getIndexWithCIID:(int)ciid
{
    for (int i = 0; i < m_ArrCircleIndicator.count; i++)
    {
        CircleIndicator *ci = [m_ArrCircleIndicator objectAtIndex:i];
        if (ci._CIID == ciid)
            return i;
    }
    
    return 0;
}

-(float) getRadiusWithCIID:(int)ciid
{
    for (int i = 0; i < m_ArrCircleIndicator.count; i++)
    {
        CircleIndicator *ci = [m_ArrCircleIndicator objectAtIndex:i];
        if (ci._CIID == ciid)
            return ci._Radius;
    }
    
    return 0.0f;
}

-(void) testAppearAll
{
    for (int i = 0; i < m_ArrCircle.count; i++)
    {
        Circle *c = [m_ArrCircle objectAtIndex:i];
        if (c._isDestroy) continue;
        
        [c appear];
    }
}

-(void) dealloc
{
    [super dealloc];

    [m_ArrCircle removeAllObjects];
    [m_ArrCircle release];
    m_ArrCircle = nil;
    
    [m_ArrCircleIndicator removeAllObjects];
    [m_ArrCircleIndicator release];
    m_ArrCircleIndicator = nil;
}

@end
